package cn.iocoder.yudao.module.service.controller.admin.commonphrase;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

import cn.iocoder.yudao.module.service.controller.admin.commonphrase.vo.*;
import cn.iocoder.yudao.module.service.dal.dataobject.commonphrase.CommonPhraseDO;
import cn.iocoder.yudao.module.service.service.commonphrase.CommonPhraseService;

@Tag(name = "管理后台 - 常用语")
@RestController
@RequestMapping("/service/common-phrase")
@Validated
public class CommonPhraseController {

    @Resource
    private CommonPhraseService commonPhraseService;

    @PostMapping("/create")
    @Operation(summary = "创建常用语")
    public CommonResult<Long> createCommonPhrase(@Valid @RequestBody CommonPhraseSaveReqVO createReqVO) {
        createReqVO.setAssistantId(getLoginUserId());
        return success(commonPhraseService.createCommonPhrase(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新常用语")
    public CommonResult<Boolean> updateCommonPhrase(@Valid @RequestBody CommonPhraseSaveReqVO updateReqVO) {
        commonPhraseService.updateCommonPhrase(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除常用语")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteCommonPhrase(@RequestParam("id") Long id) {
        commonPhraseService.deleteCommonPhrase(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得常用语")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<CommonPhraseRespVO> getCommonPhrase(@RequestParam("id") Long id) {
        CommonPhraseDO commonPhrase = commonPhraseService.getCommonPhrase(id);
        return success(BeanUtils.toBean(commonPhrase, CommonPhraseRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得常用语列表")
    public CommonResult<List<CommonPhraseRespVO>> getCommonPhrasePage() {
        @Valid CommonPhrasePageReqVO pageReqVO = new CommonPhrasePageReqVO();
        pageReqVO.setAssistantId(getLoginUserId());
        List<CommonPhraseDO> pageResult = commonPhraseService.getCommonPhrasePage(pageReqVO).getList();
        return success(BeanUtils.toBean(pageResult, CommonPhraseRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得常用语分页")
    public CommonResult<PageResult<CommonPhraseRespVO>> getCommonPhrasePage(@Valid CommonPhrasePageReqVO pageReqVO) {
        PageResult<CommonPhraseDO> pageResult = commonPhraseService.getCommonPhrasePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CommonPhraseRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出常用语 Excel")
    @OperateLog(type = EXPORT)
    public void exportCommonPhraseExcel(@Valid CommonPhrasePageReqVO pageReqVO,
                                        HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CommonPhraseDO> list = commonPhraseService.getCommonPhrasePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "常用语.xls", "数据", CommonPhraseRespVO.class,
                BeanUtils.toBean(list, CommonPhraseRespVO.class));
    }

}